package ru.acme.lab12.domain.account;


public class CheckingAccount extends AbstractAccount {

	private double overdraft;
	
	public int hashCode() {
		final int prime = 31;
		int result = super.hashCode();
		result = prime * result + Double.valueOf(overdraft).hashCode();
		return result;
	}

	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		CheckingAccount other = (CheckingAccount) obj;
		if (overdraft != other.overdraft)
			return false;
		return super.equals((AbstractAccount)obj);
	}

	public String toString() {
		return super.toString() + ",overdraft=" + overdraft;
	}

	public CheckingAccount(double balance, double overdraft) {
		super(balance);
		this.overdraft = overdraft;
	}

	public boolean withdraw(double amount) {
		if (super.getBalance() + overdraft >= amount) {
			if (super.getBalance() >= amount) {
				super.withdraw(amount);
			} else {
				amount -= super.getBalance();
				super.withdraw(super.getBalance());
				overdraft -= amount;
			}
			assert (overdraft >= 0)&& (super.getBalance() >= 0) : "Error";
			return true;
		}
		return false;
	}

	public double maximumAmountToWihdraw() {
		return overdraft + super.maximumAmountToWihdraw();
	}

}
